<template>
  <el-dialog :title="!dataForm.id ? '商品调拨单新增':'商品调拨单修改'" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="申请部门" prop="outDeptId">
            <el-select v-model="dataForm.outDeptId" placeholder="请选择" @change="outDeptChange($event)">
              <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="接收部门" prop="inDeptId">
            <el-select v-model="dataForm.inDeptId" placeholder="请选择" @change="inDeptChange($event)">
              <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8"></el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :span="24">
          <el-form-item label="备注" prop="stockId">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="dataForm.memo"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <el-form :inline="true">
      <el-form-item><el-button type="primary" @click="addGoodsBySingle">单品添加商品</el-button></el-form-item>
      <el-form-item><el-button type="primary">条码扫描</el-button></el-form-item>
      <el-form-item style="float: right;"><el-button type="danger" @click="delTableData">删除</el-button></el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" header-align="center" align="center" width="55"></el-table-column>
      <el-table-column prop="barcode" header-align="center" align="center" label="商品代码"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="商品名称"></el-table-column>
      <el-table-column prop="spec" header-align="center" align="center" label="型号"> </el-table-column>
      <el-table-column prop="applyQuantity" header-align="center" align="center" label="数量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.applyQuantity" :ref="'number'+scope.$index" @keyup.enter.native="enterChange(scope.$index)"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保存</el-button>
      <el-switch v-model="dataForm.applyStatus" active-text="保存并审核" inactive-value="0" active-value="1" style="margin-left: 15px;"></el-switch>
    </span>
    <goods-list v-if="goodsListVisible" ref="sinleGoodsList" @updateForm="updateFormData"></goods-list>
  </el-dialog>
</template>

<script>
    import {saveNewCheckOrder,saveAndAuditOrder,getStockDataList,getCtgDataList} from '@/api/eims/check'
    import {allocationApplyAdd,allocationApplyGetObj,allocationApplyEdit} from '@/api/eims/allocation-apply'
    import {getDeptInfo} from '@/api/admin/dept'
    // import GoodsList from './list.vue'
    import GoodsList from '@/components/business/goods-search-single'
    export default {
    data () {
      return {
        visible: false,
				ischeck: false,
        dataForm: {
          id: 0,
          applyStatus: '',
          outDeptId: '',
          outDeptName: '',
          inDeptId: '',
          inDeptName: '',
          memo: '',
          detailList: []
        },
        deptOptions: [],
        dataList: [],
        dataListLoading: false,
        dataRule: {outDeptId: [{ required: true,message: '请选择申请部门'}],inDeptId: [{required:true,message: '请选择接收部门'}]},
        goodsListVisible: false,
        selectionList: []
      }
    },
    components: { GoodsList },
    methods: {
      init (id) {
        this.getDeptList()
        this.dataForm.id = id || 0
        this.visible = true
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            allocationApplyGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data.data
                this.dataList = response.data.data.data.detailList
                this.dataList.forEach(item => {
                  item.barcode = item.goods.barcode
                  item.name = item.goods.name
                  item.spec = item.goods.spec
                })
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      outDeptChange($event) {
        this.deptOptions.forEach(item => { if(item.id == $event){ this.dataForm.outDeptName = item.name } })
      },
      inDeptChange($event) {
        this.deptOptions.forEach(item => { if(item.id == $event){ this.dataForm.inDeptName = item.name } })
      },
      // 打开商品列表弹窗
      addGoodsBySingle() {
        let data = { params: {}, data: this.dataList }
        this.goodsListVisible = true
        this.$nextTick(() => {  this.$refs.sinleGoodsList.init(data) })
      },
      handleSelectionChange(val) {
        this.selectionList = val
      },
      //  回车切换下一行商品数量
      enterChange(index){
        if(this.$refs['number'+(index*1 + 1)]){
          this.$refs['number'+(index*1 + 1)].focus()
        }
      },
      // 删除选中的商品
      delTableData() {
        this.selectionList.forEach((item,index) => {
          for(let i = 0; i < this.dataList.length; i++){
            if(this.dataList[i] == item){
              this.dataList.splice(i,1)
            }
          }
          this.selectionList = []
        })
      },
      //更新表单数据
      updateFormData(data) {
        this.dataList = JSON.parse( JSON.stringify(data) )
        let list = []
        data.forEach(item => {
          item.goodsId = item.id
          list.push(item)
        })
        this.dataForm.detailList = list
      },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.detailList.forEach((item,index) => { item.applyQuantity = this.dataList[index].applyQuantity })
            allocationApplyAdd(this.dataForm).then(response => {
              if(response.data.code == '0'){
               (this.dataForm.applyStatus == '1') ? this.$message.success('新增并审核成功') : this.$message.success('新增成功')
               this.visible = false
               this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
						  this.ischeck = false
            }).catch(() => {
							this.ischeck = false
						})
          }else{
            this.ischeck = false
          }
        })
      },

      /*
      * *  获取所需数据字典
      */
     getDeptList(){
       getDeptInfo().then(result => {
         this.deptOptions = result.data.data
       })
     }
    }
  }
</script>
